/**
 * @desc 日志
 */
<template>
    <view class="logs">
        <nut-backtop v-if="logs.length">
            <template v-slot:content>
                <view class="logs-item" v-for="item in logs" :key="item._id">
                    <text>{{ getTime(item.createTime) }}</text>
                    <text class="logs-item-text">{{ item.done ? '预约成功' : '取消预约' }}</text>
                </view>
            </template>
        </nut-backtop>
        <nut-empty v-else description="无数据"></nut-empty>
    </view>
</template>

<script>
import db from '../../server/index';
import Taro from '@tarojs/taro';
const openId = Taro.getStorageSync('openid')
export default {
    name: 'Logs',
    data() {
        return {
            logs: [],
            coachId: ''
        }
    },
    onPullDownRefresh() {
        Taro.showLoading({
            title: '加载中',
        })
        this.init()
    },
    created() {
        db.collection('phone').where({
            _openid: openId
        }).get().then(({ data = [] }) => {
            this.coachId = data[0].coachId;
            this.init();
        })
    },
    methods: {
        getTime(time) {
            const date = new Date(time);
            let year = date.getFullYear();
            let month = (date.getMonth() + 1 + '').padStart(2, 0);
            let day = (date.getDate() + '').padStart(2, 0)
            let hour = (date.getHours() + '').padStart(2, 0)
            let minutes = (date.getMinutes() + '').padStart(2, 0)
            let second = (date.getSeconds() + '').padStart(2, 0)
            return `${year}-${month}-${day} ${hour}:${minutes}:${second}`
        },
        init() {
            db.collection('user').where({
                _openid: openId,
                coachId: this.coachId
            }).get().then(({ data = [] }) => {
                data = data.sort((a, b) => {
                    return a.createTime - b.createTime > 0 ? -1 : 1
                })
                this.logs = data
                Taro.stopPullDownRefresh();
                Taro.hideLoading();
            })
        }
    },
}
</script>

<style lang="less">
.logs {
    &-item {
        height: 50px;
        display: flex;
        padding: 0 5px;
        align-items: center;
        justify-content: space-between;

        &-text {
            font-size: 14px;
        }
    }
}
</style>
